import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
import React, { useState, useImperativeHandle, useEffect, useMemo, useRef, } from "react";
import { TextBox } from "devextreme-react";
const PTextBox = React.forwardRef((props, ref) => {
    const { defaultValue } = props;
    const [value, setValue] = useState("");
    const boxValue = useRef("");
    const textBoxRef = useRef();
    useEffect(() => {
        setValue(defaultValue);
        boxValue.current = defaultValue;
    }, [defaultValue]);
    const handleChange = (e) => {
        // console.log(e, '@@@');
        const { event } = e;
        // console.log(event.target.value, '@@@');
        boxValue.current = event.target.value;
        setValue(event.target.value);
        props.changeData(event.target.value);
    };
    const findValue = () => {
        // console.log(boxValue.current, '@@@');
        return boxValue.current;
    };
    useImperativeHandle(ref, () => ({
        initValue: (value) => setValue(value),
        getValue: () => findValue(),
        focusTextBox: () => {
            if (textBoxRef.current) {
                textBoxRef.current.instance.focus();
            }
        },
    }));
    const handleKeyDown = (e) => {
        if (e.event.keyCode === 13) {
            e.event.preventDefault();
            if (props?.onKeyDownTextBox) {
                props?.onKeyDownTextBox();
            }
        }
    };
    return (_jsx(_Fragment, { children: useMemo(() => {
            return (_jsx(TextBox, { ref: textBoxRef, value: value, onChange: handleChange, onKeyDown: handleKeyDown }));
        }, [value]) }));
});
export default PTextBox;
